<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use app\common\library\Page;
use think\Db;

/**
 * 日记控制器
 * Class Diary
 * @package app\index\controller
 */
class Diary extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    /**
     * 小日子
     * @param $id
     * @return string
     * @throws \think\Exception
     */
    public function index()
    {
        $id=!empty($this->request->param('id'))?intval($this->request->param('id')):0;
        if(empty($id))$this->error('文章分类不存在！',url('/index'));

        $cpage=!empty($this->request->param('cpage'))?intval($this->request->param('cpage')):1;
        $total=!empty($this->request->param('total'))?intval($this->request->param('total')):12;
        $cid=0;

        //文章分类信息作为页面信息
        $pageInfo=db('article_category')->where('id='.$id)->field('id,title,keywords,description,name,info,pid')->find();
        if(empty($pageInfo)) $this->error('文章分类不存在！',url('/index'));

        //根据当前控制器标识查询顶级导航信息
        $parentNavigation=db('navigation')->where('controllername="'.$this->identify.'" and pid=0')->field('name')->find();

        //分类下的子分类
        $children=db('article_category')->where('pid='.$id)->order('sort desc,id asc')->field('id,name,pid')->select();

        //分类下所有文章
        if(!empty($children)){
            $cid=!empty($this->request->param('cid'))?intval($this->request->param('cid')):$children[0]['id'];
            $start=($cpage-1)*$total;
            $url='/diary/'.$id.'/'.$cid;

            $articleList=db('article')->where('category_id='.$cid)->order('sort desc,id desc')->field('id,title,thumb,info,from_unixtime(createtime,"%Y-%m-%d") as createtime')->limit($start,$total)->select();
            $count=db('article')->where('category_id='.$cid)->count();

            //分页
            $page=new Page();
            $show=$page->create($count,$cpage,$total,$url);

            $this->assign('articleList',$articleList);
            $this->assign('show',$show);
        }

        //标签云 随机20个
        $tagsList=Db::query("select id,name from fa_tags order by rand() limit 50");

        //文章点击排行
        $clicksSortArticleList=db('article')->order('clicks desc,sort desc,id desc')->field('id,title')->limit(8)->select();

        //合作伙伴
        $cooperativePartnerList=db('cooperative_partner')->where('type=0')->order('sort desc,id desc')->field('name,link')->limit(8)->select();

        //父分类下的子分类
        $childrenPlist=db('article_category')->where('pid='.$pageInfo['pid'])->order('sort desc,id asc')->field('id,name,pattem')->limit(4)->select();
        if(!empty($childrenPlist[0])){
            $childrenPlist[0]['link']=url('/diary/'.$childrenPlist[0]['id']);
            $childrenPlist[0]['class']='small-title';

            if(!empty($childrenPlist[1])){
                $childrenPlist[1]['link']=url('/video/'.$childrenPlist[1]['id']);
                $childrenPlist[1]['class']='video-title';
            }

            if(!empty($childrenPlist[2])){
                $childrenPlist[2]['link']=url('/note/'.$childrenPlist[2]['id']);
                $childrenPlist[2]['class']='note-title';
            }

            if(!empty($childrenPlist[3])){
                $childrenPlist[3]['link']=url('/comprehensive/'.$childrenPlist[3]['id']);
                $childrenPlist[3]['class']='comprehensive-title';
            }

        }

        $this->assign('pageInfo',$pageInfo);
        $this->assign('parentNavigation',$parentNavigation);
        $this->assign('children',$children);
        $this->assign('tagsList',$tagsList);
        $this->assign('clicksSortArticleList',$clicksSortArticleList);
        $this->assign('cooperativePartnerList',$cooperativePartnerList);
        $this->assign('childrenPlist',$childrenPlist);
        $this->assign('id',$id);
        $this->assign('cid',$cid);
        return $this->view->fetch();
    }

    /**
     * 短视频
     * @param $id
     * @return string
     * @throws \think\Exception
     */
    public function video(){
        $id=!empty($this->request->param('id'))?intval($this->request->param('id')):0;
        if(empty($id))$this->error('文章分类不存在！',url('/index'));

        $cpage=!empty($this->request->param('cpage'))?intval($this->request->param('cpage')):1;
        $total=!empty($this->request->param('total'))?intval($this->request->param('total')):6;
        $cid=0;

        //文章分类信息作为页面信息
        $pageInfo=db('article_category')->where('id='.$id)->field('title,keywords,description,name,info,pid')->find();
        if(empty($pageInfo)){
            $this->error('文章分类不存在！',url('/index'));
        }

        //根据当前控制器标识查询顶级导航信息
        $parentNavigation=db('navigation')->where('controllername="'.$this->identify.'" and pid=0')->field('name')->find();

        //分类下的子分类
        $children=db('article_category')->where('pid='.$id)->order('sort desc,id asc')->field('id,name,pid')->select();

        //分类下所有文章
        if(!empty($children)){
            $cid=!empty($this->request->param('cid'))?intval($this->request->param('cid')):$children[0]['id'];
            $start=($cpage-1)*$total;
            $url='/video/'.$id.'/'.$cid;

            $articleList=db('article')->where('category_id='.$cid)->order('sort desc,id desc')->field('id,title,video_url,info,from_unixtime(createtime,"%Y-%m-%d") as createtime')->limit($start,$total)->select();
            $count=db('article')->where('category_id='.$cid)->count();

            //分页
            $page=new Page();
            $show=$page->create($count,$cpage,$total,$url);

            $this->assign('articleList',$articleList);
            $this->assign('show',$show);
        }

        //父分类下的子分类
        $childrenPlist=db('article_category')->where('pid='.$pageInfo['pid'])->order('sort desc,id asc')->field('id,name,pattem')->limit(4)->select();
        if(!empty($childrenPlist[0])){
            $childrenPlist[0]['link']=url('/diary/'.$childrenPlist[0]['id']);
            $childrenPlist[0]['class']='small-title';

            if(!empty($childrenPlist[1])){
                $childrenPlist[1]['link']=url('/video/'.$childrenPlist[1]['id']);
                $childrenPlist[1]['class']='video-title';
            }

            if(!empty($childrenPlist[2])){
                $childrenPlist[2]['link']=url('/note/'.$childrenPlist[2]['id']);
                $childrenPlist[2]['class']='note-title';
            }

            if(!empty($childrenPlist[3])){
                $childrenPlist[3]['link']=url('/comprehensive/'.$childrenPlist[3]['id']);
                $childrenPlist[3]['class']='comprehensive-title';
            }

        }

        $this->assign('pageInfo',$pageInfo);
        $this->assign('parentNavigation',$parentNavigation);
        $this->assign('children',$children);
        $this->assign('childrenPlist',$childrenPlist);
        $this->assign('id',$id);
        $this->assign('cid',$cid);
        return $this->view->fetch();
    }

    /**
     * 读书笔记
     * @param $id
     * @return string
     * @throws \think\Exception
     */
    public function note(){
        $id=!empty($this->request->param('id'))?intval($this->request->param('id')):0;
        if(empty($id))$this->error('文章分类不存在！',url('/index'));

        $cpage=!empty($this->request->param('cpage'))?intval($this->request->param('cpage')):1;
        $total=!empty($this->request->param('total'))?intval($this->request->param('total')):8;
        $cid=0;

        //文章分类信息作为页面信息
        $pageInfo=db('article_category')->where('id='.$id)->field('title,keywords,description,name,info,pid')->find();
        if(empty($pageInfo)){
            $this->error('文章分类不存在！',url('/index'));
        }

        //根据当前控制器标识查询顶级导航信息
        $parentNavigation=db('navigation')->where('controllername="'.$this->identify.'" and pid=0')->field('name')->find();

        //分类下的子分类
        $children=db('article_category')->where('pid='.$id)->order('sort desc,id asc')->field('id,name,pid')->select();

        //分类下所有文章
        if(!empty($children)){
            $cid=!empty($this->request->param('cid'))?intval($this->request->param('cid')):$children[0]['id'];
            $start=($cpage-1)*$total;
            $url='/note/'.$id.'/'.$cid;

            $articleList=db('article')->where('category_id='.$cid)->order('sort desc,id desc')->field('id,title,thumb,info,from_unixtime(createtime,"%Y-%m-%d") as createtime,avatar,isding')->limit($start,$total)->select();
            $count=db('article')->where('category_id='.$cid)->count();

            //分页
            $page=new Page();
            $show=$page->create($count,$cpage,$total,$url);

            $this->assign('articleList',$articleList);
            $this->assign('show',$show);

        }

        //父分类下的子分类
        $childrenPlist=db('article_category')->where('pid='.$pageInfo['pid'])->order('sort desc,id asc')->field('id,name,pattem')->limit(4)->select();
        if(!empty($childrenPlist[0])){
            $childrenPlist[0]['link']=url('/diary/'.$childrenPlist[0]['id']);
            $childrenPlist[0]['class']='small-title';

            if(!empty($childrenPlist[1])){
                $childrenPlist[1]['link']=url('/video/'.$childrenPlist[1]['id']);
                $childrenPlist[1]['class']='video-title';
            }

            if(!empty($childrenPlist[2])){
                $childrenPlist[2]['link']=url('/note/'.$childrenPlist[2]['id']);
                $childrenPlist[2]['class']='note-title';
            }

            if(!empty($childrenPlist[3])){
                $childrenPlist[3]['link']=url('/comprehensive/'.$childrenPlist[3]['id']);
                $childrenPlist[3]['class']='comprehensive-title';
            }

        }

        //站长推荐
        $recommendArticleList=db('article')->where('isrecommend=1')->order('sort desc,id desc')->field('id,title')->limit(8)->select();

        //点击排行
        $clicksSortArticleList=db('article')->order('clicks desc,sort desc,id desc')->field('id,title')->limit(8)->select();

        //标签云 随机20个
        $tagsList=Db::query("select id,name from fa_tags order by rand() limit 50");

        $this->assign('pageInfo',$pageInfo);
        $this->assign('parentNavigation',$parentNavigation);
        $this->assign('children',$children);
        $this->assign('childrenPlist',$childrenPlist);
        $this->assign('recommendArticleList',$recommendArticleList);
        $this->assign('clicksSortArticleList',$clicksSortArticleList);
        $this->assign('tagsList',$tagsList);
        $this->assign('id',$id);
        $this->assign('cid',$cid);
        return $this->view->fetch();
    }

    public function comprehensive(){
        $id=!empty($this->request->param('id'))?intval($this->request->param('id')):0;
        if(empty($id))$this->error('文章分类不存在！',url('/index'));

        $cpage=!empty($this->request->param('cpage'))?intval($this->request->param('cpage')):1;
        $total=!empty($this->request->param('total'))?intval($this->request->param('total')):8;
        $cid=0;

        //文章分类信息作为页面信息
        $pageInfo=db('article_category')->where('id='.$id)->field('title,keywords,description,name,info,pid')->find();
        if(empty($pageInfo)){
            $this->error('文章分类不存在！',url('/index'));
        }

        //根据当前控制器标识查询顶级导航信息
        $parentNavigation=db('navigation')->where('controllername="'.$this->identify.'" and pid=0')->field('name')->find();

        //分类下的子分类
        $children=db('article_category')->where('pid='.$id)->order('sort desc,id asc')->field('id,name,pid')->select();

        //分类下所有文章
        if(!empty($children)){
            $cid=!empty($this->request->param('cid'))?intval($this->request->param('cid')):$children[0]['id'];
            $start=($cpage-1)*$total;
            $url='/comprehensive/'.$id.'/'.$cid;

            $articleList=db('article')->where('category_id='.$cid)->order('sort desc,id desc')->field('id,title,thumb,from_unixtime(createtime,"%Y-%m-%d %H:%i:%s") as createtime,avatar,author')->limit($start,$total)->select();
            $count=db('article')->where('category_id='.$cid)->count();

            //分页
            $page=new Page();
            $show=$page->create($count,$cpage,$total,$url);

            $this->assign('articleList',$articleList);
            $this->assign('show',$show);
        }

        //父分类下的子分类
        $childrenPlist=db('article_category')->where('pid='.$pageInfo['pid'])->order('sort desc,id asc')->field('id,name,pattem')->limit(4)->select();
        if(!empty($childrenPlist[0])){
            $childrenPlist[0]['link']=url('/diary/'.$childrenPlist[0]['id']);
            $childrenPlist[0]['class']='small-title';

            if(!empty($childrenPlist[1])){
                $childrenPlist[1]['link']=url('/video/'.$childrenPlist[1]['id']);
                $childrenPlist[1]['class']='video-title';
            }

            if(!empty($childrenPlist[2])){
                $childrenPlist[2]['link']=url('/note/'.$childrenPlist[2]['id']);
                $childrenPlist[2]['class']='note-title';
            }

            if(!empty($childrenPlist[3])){
                $childrenPlist[3]['link']=url('/comprehensive/'.$childrenPlist[3]['id']);
                $childrenPlist[3]['class']='comprehensive-title';
            }

        }

        //点击排行
        $clicksSortArticleList=db('article')->order('clicks desc,sort desc,id desc')->field('id,title')->limit(8)->select();

        //标签云 随机20个
        $tagsList=Db::query("select id,name from fa_tags order by rand() limit 20");

        //合作伙伴
        $cooperativePartnerList=db('cooperative_partner')->where('type=0')->order('sort desc,id desc')->field('name,link')->limit(8)->select();

        $this->assign('pageInfo',$pageInfo);
        $this->assign('parentNavigation',$parentNavigation);
        $this->assign('children',$children);
        $this->assign('childrenPlist',$childrenPlist);
        $this->assign('clicksSortArticleList',$clicksSortArticleList);
        $this->assign('tagsList',$tagsList);
        $this->assign('cooperativePartnerList',$cooperativePartnerList);
        $this->assign('id',$id);
        $this->assign('cid',$cid);
        return $this->view->fetch();
    }

}